package com.gooddare.view.component 
{
	import com.gooddare.controller.ApplicationFacade;
	import com.gooddare.locator.AssetsLocator;
	import com.gooddare.model.ModelLocator;
	import com.riatimes.mvc.view.BaseComponent;
	import com.riatimes.util.Reflection;
	import com.riatimes.util.UIUtil;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import com.greensock.easing.Quad;
	import com.greensock.TweenMax;
	/**
	 * ...
	 * @author Lyleff
	 */
	public class MainMenuComponent extends BaseComponent
	{
		private var bm:Bitmap;
		private var na:String;//名字
		private var mm:Sprite;
		private var iconCont:Sprite;
		private var iconMask:Sprite;
		private var isSelect:Boolean = false;

		public function MainMenuComponent(bm:Bitmap,na:String) 
		{
			this.bm = bm;
			this.na = na;
		}
		override public function initUI():void {
			mm = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_INDEX_MAINMENU_UI) as Sprite;
			this.addChild(mm);
			
			iconCont = UIUtil.getElement(mm, "iconCont") as Sprite;
			iconMask = UIUtil.getElement(mm, "iconMask") as Sprite;
			iconCont.addChild(bm);
			this.filters = [new GlowFilter(0xffffff,0,0,0)];
		}
		override public function initLocation():void {
			
		}
		override public function initEvent (b:Boolean = true) : void {
			if (b) {
				this.addEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
				this.addEventListener(MouseEvent.ROLL_OUT, __mouseRollEventHandle);
				
				this.addEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
			}else {
				this.removeEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
				this.removeEventListener(MouseEvent.ROLL_OUT, __mouseRollEventHandle);
				
				this.removeEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
			}
		}
		
		private function __mouseClickEventHandle(e:MouseEvent):void 
		{
			var cur:Object = ModelLocator.mainMenuCur;
			if(cur["type_"]){
				if(cur["type_"]==this){
					//cur["type_"]=null;
				}else {
					cur["type_"].setSelect(false);
					cur["type_"]=this;
					cur["type_"].setSelect(true);
				}
			}else{
				cur["type_"]=this;
				cur["type_"].setSelect(true);
			}
		}
		public function mainSelect():void {
			var cur:Object = ModelLocator.mainMenuCur;
			cur["type_"]=this;
			cur["type_"].setSelect(true);
		}
		public function setSelect(b:Boolean):void {
			if (b) {
				TweenMax.to(iconMask, 0.5, { alpha:0, ease:Quad.easeOut } );
				TweenMax.to(this, 0.5, { glowFilter: { color:0x333333, alpha:1, blurX:3, blurY:3, ease:Quad.easeOut }} );
				isSelect = true;
				
				var facade:ApplicationFacade = ApplicationFacade.getInstance();
				facade.sendNotification(ApplicationFacade.EVENT_APP_MAINMENU_CLICK,na);
			}else {
				tweenSelect(b);
				isSelect = false;
			}
		}
		private function tweenSelect(b:Boolean):void {
			if (b) {
				TweenMax.to(iconMask, 0.5, { alpha:0, ease:Quad.easeOut } );
				TweenMax.to(this, 0.5, { glowFilter: { color:0x999999, alpha:1, blurX:3, blurY:3, ease:Quad.easeOut }} );
			}else {
				TweenMax.to(iconMask, 0.5, { alpha:0.1, ease:Quad.easeIn } );
				TweenMax.to(this, 0.5, { glowFilter: { color:0xffffff,alpha:0, blurX:0, blurY:0, ease:Quad.easeIn }} );
			}
		}
		private function __mouseRollEventHandle(e:MouseEvent):void 
		{
			switch(e.type) {
				case MouseEvent.ROLL_OVER:
					if (!isSelect) {
						tweenSelect(true);
					}
				break;
				
				case MouseEvent.ROLL_OUT:
					if (!isSelect) {
						tweenSelect(false);
					}
				break;
				

			}
		}
	}

}